
#include "NanoWinCore.h"

using namespace Nano;

LRESULT CALLBACK NanoWinCore::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	
	NanoWinCore *parent = NULL;
	if(msg == WM_CREATE) {
		parent = (NanoWinCore*)((LPCREATESTRUCT)lParam)->lpCreateParams;
		SetWindowLongPtr(hwnd,GWL_USERDATA,(LONG_PTR)parent);
	} else {
		parent = (NanoWinCore*)GetWindowLongPtr(hwnd,GWL_USERDATA);
		if(!parent) return DefWindowProc(hwnd,msg,wParam,lParam);
	}	
	
	
	switch(msg)
	{
		case WM_SIZE:
			parent->Layout();
		break;
		case WM_CREATE:
		break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_COMMAND:
	        switch(LOWORD(wParam))
    	    {
		    case ID_FILE_NEW:
				parent->createNewDocument();
			break;
        	    case ID_FILE_EXIT:
        	        PostMessage(hwnd, WM_CLOSE, 0, 0);
        	    break;
        	    case ID_STUFF_GO:

        	    break;
        	}
    break;		
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

void NanoWinCore::createDocumentView(Document *document) {
	HFONT hFont = (HFONT)GetStockObject( DEFAULT_GUI_FONT );
	TCITEM tie;
	tie.mask = TCIF_TEXT;
	tie.pszText = "Untitled";
	if (TabCtrl_InsertItem(documentTabber, 0, &tie) == -1){
		MessageBox(NULL, "Couldn't add Tab1", "Tab Example", MB_OK | MB_ICONERROR);
	}        

	editorWindow = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		RICHEDIT_CLASS,
		"",
		WS_CHILD | WS_VISIBLE | 
		WS_VSCROLL | WS_HSCROLL | ES_AUTOHSCROLL | ES_MULTILINE | ES_WANTRETURN,
		0, 0, 600,800,
		documentTabber, NULL, NULL, NULL);

	SendMessage( editorWindow, WM_SETFONT, (WPARAM)hFont, TRUE );
	SendMessage( editorWindow, EM_SETBKGNDCOLOR, 0, RGB(0x20,0x20,0x20));

	CHARFORMAT2 format;
	memset( &format, 0, sizeof(format));
	format.dwMask = CFM_COLOR|CFM_BACKCOLOR|CFM_FACE;
	format.crTextColor = RGB(0xDC,0xDC,0xDC);
	format.crBackColor = RGB(0x30,0x30,0x30);
	//strcpy(format.szFaceName, "Fixedsys");
	strcpy(format.szFaceName, "Courier");
	format.cbSize = sizeof(format);
	SendMessage( editorWindow, EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&format);
	
	Layout();
}

NanoWinCore::NanoWinCore(HINSTANCE hInstance, int nCmdShow) : NanoCore() {

	LoadLibrary("RichEd20.dll");
	LoadLibrary("comctl32.dll");

	editorWindow = NULL;

	HFONT hFont = (HFONT)GetStockObject( DEFAULT_GUI_FONT );

	wc.cbSize	= sizeof(WNDCLASSEX);
	wc.style	= 0;
	wc.lpfnWndProc  = NanoWinCore::WndProc;
	wc.cbClsExtra	= 0;
	wc.cbWndExtra	= 0;
	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);	
	wc.hInstance	= hInstance;
	wc.hIcon	= LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor	= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
	wc.lpszClassName = "NanoCoreClass";
	wc.hIconSm	 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
	}
	
	hwnd = CreateWindowEx(
		WS_EX_APPWINDOW|WS_EX_WINDOWEDGE,
		"NanoCoreClass",
		"NanoPad",
		WS_BORDER|WS_CAPTION|WS_SYSMENU|WS_SIZEBOX|WS_MAXIMIZEBOX|WS_MINIMIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT, 600, 700,
		NULL, NULL, hInstance, this);

	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
	}
	
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);


	documentTabber = CreateWindowEx(
        0,                      // extended style
        WC_TABCONTROL,          // tab control constant
        "",                     // text/caption
        WS_CHILD | WS_VISIBLE,  // is a child control, and visible
        5,                      // X position - device units from left
        5,                      // Y position - device units from top
        585,                    // Width - in device units
        685,                    // Height - in device units
        hwnd,            // parent window
        NULL,                   // no menu
        hInstance,                 // instance
        NULL                    // no extra junk
        );
	
	SendMessage( documentTabber, WM_SETFONT, (WPARAM)hFont, TRUE );	

	Layout();
}

void NanoWinCore::Layout() {
	RECT wRect;
	RECT tr;

	GetClientRect(hwnd, &wRect);
	MoveWindow(documentTabber,5, 5, wRect.right-10, wRect.bottom - 10, true);	
	
	if(editorWindow) {
		GetClientRect(documentTabber, &wRect);
		TabCtrl_GetItemRect(documentTabber, 0, &tr);

		MoveWindow(editorWindow,wRect.left+2, (tr.bottom - tr.top) + 5, wRect.right-5, wRect.bottom - tr.bottom - 5, true);
	}
}

NanoWinCore::~NanoWinCore() {
	
}

bool NanoWinCore::Update() {
	MSG Msg;	
	bool retVal = (GetMessage(&Msg, NULL, 0, 0) > 0);
	TranslateMessage(&Msg);
	DispatchMessage(&Msg);
	return retVal;
}
